<?php
/**
 * User
 *
 * PHP version 5
 *
 * @category Controller
 * @version  1.0
 * @author   Duonght <hduongdmit@gmail.com>
 */
App::uses('AppController', 'Controller');
class AdvertiseController extends AppController {

	public $uses = array('Advertise');
	/**
	 * Components
	 * @var array
	 */
	public $components = array(
			'Paginator',
			'Upload',
	);
	public function beforeFilter(){
		parent::beforeFilter();
		$banner_type = Configure::read('banner_type');
		$target      = Configure::read('target');

		$this->set('target', $target);
		$this->set('banner_type', $banner_type);
	}

	public function index(){
		$conditions = array('del_flg' => 0);
		$paginate = array(
				'fields' => '*',
				'limit' => ConstAdminComponent::PAGE_NUMBER_LIMIT,
				'conditions' => $conditions,
				'order' => array(
						'ban_type' => 'ASC',
				),
		);

		$this->Paginator->settings = $paginate;
		$listNews = $this->Paginator->paginate('Advertise');
		//force view
		$this->set(array(
				'datas' => $listNews,
				'page_number_limit' => ConstAdminComponent::PAGE_NUMBER_LIMIT,
				'title_for_layout' => __('Banner'),
		));

	}

	public function add(){
		if ($this->request->is('post')) {
			$this->Advertise->create();
			$params = $this->request->data;
			if ($params['Advertise']['file']){
				$upload = $this->Upload;
				$upload->uploadImage(reset($params['Advertise']['file']), FOLDER_UPLOAD_BANNER, false);
				if ($upload->error() == ''){
					$params['Advertise']['ban_picture'] = $upload->getFileName();
					unset($params['Advertise']['file']);
				} else {
					$this->Session->setFlash(__($upload->error()), 'error');
					$this->set('params', $params);
					$this->redirect(array('action' => 'add'));
				}
			}

			if ($this->Advertise->save($params)) {
				$this->Session->setFlash(__('Add banner Success'), 'success');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Save could not be saved. Please, try again'), 'error');
			}
		}

		//forge view
		$this->set('title_for_layout',__('Add news banner'));
	}

	/**
	 * Delete banner
	 *
	 * @param string $id
	 */
	public function delete($id = null){
		$this->layout = null;
		if ($this->request->is('get')) {
			throw new MethodNotAllowedException();
		}

		$this->Advertise->read('id', $id);
		$this->Advertise->set('del_flg', 1);
		$this->Advertise->save();
		$this->Session->setFlash(__('Delete success'), 'success');
		return $this->redirect(array('action' => 'index'));

	}

	/**
	 * Update banner active
	 * @param string $id
	 * @param string $type
	 */
	public function update($id = null, $field = 'ban_active'){
		$this->layout = null;
		$this->render(false);
		if ($this->request->is('post')) {
			$value = $this->request->data['value'];
			$this->Advertise->read('id', $id);
			$this->Advertise->set($field, $value);
			$this->Advertise->save();
			echo $value;
		}
	}


}
